package cn.edu.lit.jsjxy.CourseExample.chapter4;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.lit.jsjxy.CourseExample.R;

public class MusicPlayingActivity extends AppCompatActivity implements View.OnClickListener
{

    CheckBox ch1, ch2;
    TextView txt;
    ImageButton mStopButton, mStartButton, mPauseButton;
    ProgressBar progressbar01;

    MediaPlayer mMediaPlayer;
    String sdcard_file;

    int length;
    int res_file;

    Timer timer;
    TimerTask timerTask;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_palying);

        sdcard_file = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Music/didadida.mp3";
        res_file = R.raw.lanjingling;

        /* 构建MediaPlayer对象 */
        mMediaPlayer = new MediaPlayer();

        ch1 = (CheckBox) findViewById(R.id.check1);
        ch2 = (CheckBox) findViewById(R.id.check2);
        txt = (TextView) findViewById(R.id.text1);
        mStopButton = (ImageButton) findViewById(R.id.btnStop);
        mStartButton = (ImageButton) findViewById(R.id.btnStart);
        mPauseButton = (ImageButton) findViewById(R.id.btnPause);
        progressbar01 = findViewById(R.id.progressbar01);

        mStopButton.setOnClickListener(this);
        mStartButton.setOnClickListener(this);
        mPauseButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId()) {
            case R.id.btnStart:
                startPlay();

                break;
            case R.id.btnStop:
                stopPlay();
                break;
            case R.id.btnPause:
                pausePlay();
                break;
        }
    }

    public void stopPlay()
    {
        /* 是否正在播放 */
        if (mMediaPlayer.isPlaying()) {
            //重置MediaPlayer到初始状态
            mMediaPlayer.reset();
            mMediaPlayer.release();
            mStartButton.setEnabled(true);
        }
    }

    public void startPlay()
    {
        String str = "";
        if (ch1.isChecked()) {
            str = str + "\n" + ch1.getText();
            try {
                mMediaPlayer = MediaPlayer.create(MusicPlayingActivity.this, res_file);
                mMediaPlayer.start();

                mStartButton.setEnabled(false);
            } catch (Exception e) {
                Log.e("ch1", e.getMessage());
            }
        }
        if (ch2.isChecked()) {
            str = str + "\n" + ch2.getText();
            try {

                playMusic(sdcard_file);
                mStartButton.setEnabled(false);

            } catch (Exception e) {
                Log.i("ch2", "sdcard err .... ");
            }
        }
        txt.setText(str);
    }

    public void pausePlay()
    {
        if (mMediaPlayer.isPlaying()) {
            /* 暂停 */
            mMediaPlayer.pause();

        } else {
            /* 开始播放 */
            mMediaPlayer.start();
        }
    }

    //播放SD卡或其他路径的音乐文件
    private void playMusic(String path)
    {
        try {
            /* 重置MediaPlayer对象，使之处于空闲状态 */
            mMediaPlayer.reset();
            /* 设置要播放的文件的路径 */
            mMediaPlayer.setDataSource(path);

            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
            {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer)
                {
                    /* 开始播放 */
                    mMediaPlayer.start();
                }
            });

            /* 准备播放 */
            mMediaPlayer.prepare();

        } catch (IOException e) {
            Log.e("MusicPlayingActivity", e.getMessage());
        }
    }
}
